package ge.telasi.tasks.model.validation;

import ge.telasi.tasks.LocaleManager;
import ge.telasi.tasks.TaskException;

/**
 * Text range validator.
 *
 * @author dimitri
 */
public class TextRangeValidator extends Validator {
    int min, max;

    public TextRangeValidator(int min, int max) {
        this.min = min;
        this.max = max;
    }

    public void validate(String name, String value) {
        int length = value == null ? 0 : value.length();
        boolean isBelowMinimum = length < min;
        if (isBelowMinimum) {
            LocaleManager lm = getLocaleManager();
            String humanName = lm.getMessage(name);
            String msg = lm.getMessage("TextRangeValidator.min", humanName, min);
            throw new TaskException(msg, name);
        }
        boolean isOverMaximum = length > max;
        if (isOverMaximum) {
            LocaleManager lm = getLocaleManager();
            String humanName = lm.getMessage(name);
            String msg = lm.getMessage("TextRangeValidator.max", humanName, max);
            throw new TaskException(msg, name);
        }
    }

}
